<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Open a new connection to the MySQL server</title>

 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="mysqli.connect-error.html">mysqli::$connect_error</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="mysqli.debug.html">mysqli::debug</a></div>
 <div class="up"><a href="class.mysqli.html">MySQLi</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div><hr /><div id="mysqli.construct" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">mysqli::__construct</h1>
  <h1 class="refname">mysqli_connect</h1>
  <p class="verinfo">(PHP 5)</p><p class="refpurpose"><span class="refname">mysqli::__construct</span> -- <span class="refname">mysqli_connect</span> &mdash; <span class="dc-title">Open a new connection to the MySQL server</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-mysqli.construct-description">
  <h3 class="title">说明</h3>
  <p class="para">面向对象风格</p>
  <div class="constructorsynopsis dc-description">
   <span class="methodname"><strong>mysqli::__construct()</strong></span>
    ([ <span class="methodparam"><span class="type">string</span> <code class="parameter">$host</code><span class="initializer"> = ini_get(&quot;mysqli.default_host&quot;)</span></span>
   [, <span class="methodparam"><span class="type">string</span> <code class="parameter">$username</code><span class="initializer"> = ini_get(&quot;mysqli.default_user&quot;)</span></span>
   [, <span class="methodparam"><span class="type">string</span> <code class="parameter">$passwd</code><span class="initializer"> = ini_get(&quot;mysqli.default_pw&quot;)</span></span>
   [, <span class="methodparam"><span class="type">string</span> <code class="parameter">$dbname</code><span class="initializer"> = &quot;&quot;</span></span>
   [, <span class="methodparam"><span class="type">int</span> <code class="parameter">$port</code><span class="initializer"> = ini_get(&quot;mysqli.default_port&quot;)</span></span>
   [, <span class="methodparam"><span class="type">string</span> <code class="parameter">$socket</code><span class="initializer"> = ini_get(&quot;mysqli.default_socket&quot;)</span></span>
  ]]]]]] )</div>

  <p class="para rdfs-comment">过程化风格</p>
  <div class="methodsynopsis dc-description">
   <span class="type"><a href="class.mysqli.html" class="type mysqli">mysqli</a></span> <span class="methodname"><a href="function.mysqli-connect.html" class="methodname">mysqli_connect</a></span>
    ([ <span class="methodparam"><span class="type">string</span> <code class="parameter">$host</code><span class="initializer"> = ini_get(&quot;mysqli.default_host&quot;)</span></span>
   [, <span class="methodparam"><span class="type">string</span> <code class="parameter">$username</code><span class="initializer"> = ini_get(&quot;mysqli.default_user&quot;)</span></span>
   [, <span class="methodparam"><span class="type">string</span> <code class="parameter">$passwd</code><span class="initializer"> = ini_get(&quot;mysqli.default_pw&quot;)</span></span>
   [, <span class="methodparam"><span class="type">string</span> <code class="parameter">$dbname</code><span class="initializer"> = &quot;&quot;</span></span>
   [, <span class="methodparam"><span class="type">int</span> <code class="parameter">$port</code><span class="initializer"> = ini_get(&quot;mysqli.default_port&quot;)</span></span>
   [, <span class="methodparam"><span class="type">string</span> <code class="parameter">$socket</code><span class="initializer"> = ini_get(&quot;mysqli.default_socket&quot;)</span></span>
  ]]]]]] )</div>

  <p class="para rdfs-comment">
   Opens a connection to the MySQL Server running on.
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-mysqli.construct-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>

    
     <dt>
<em><code class="parameter">host</code></em></dt>

     <dd>

      <p class="para">
       Can be either a host name or an IP address. Passing the <strong><code>NULL</code></strong> value
       or the string &quot;localhost&quot; to this parameter, the local host is
       assumed. When possible, pipes will be used instead of the TCP/IP
       protocol.
      </p>
      <p class="para">
       Prepending host by <em>p:</em> opens a persistent connection.
       <span class="function"><a href="mysqli.change-user.html" class="function">mysqli_change_user()</a></span> is automatically called on
       connections opened from the connection pool.
      </p>
     </dd>

    
    
     <dt>
<em><code class="parameter">username</code></em></dt>

     <dd>

      <p class="para">
       The MySQL user name.
      </p>
     </dd>

    
    
     <dt>
<em><code class="parameter">passwd</code></em></dt>

     <dd>

      <p class="para">
       If not provided or <strong><code>NULL</code></strong>, the MySQL server will attempt to authenticate
       the user against those user records which have no password only. This
       allows one username to be used with different permissions (depending
       on if a password as provided or not).
      </p>
     </dd>

    
    
     <dt>
<em><code class="parameter">dbname</code></em></dt>

     <dd>

      <p class="para">
       If provided will specify the default database to be used when
       performing queries.
      </p>
     </dd>

    
    
     <dt>
<em><code class="parameter">port</code></em></dt>

     <dd>

      <p class="para">
       Specifies the port number to attempt to connect to the MySQL server.
      </p>
     </dd>

    
    
     <dt>
<em><code class="parameter">socket</code></em></dt>

     <dd>

      <p class="para">
       Specifies the socket or named pipe that should be used.
      </p>
      <blockquote class="note"><p><strong class="note">Note</strong>: 
       <p class="para">
        Specifying the <em><code class="parameter">socket</code></em> parameter will not
        explicitly determine the type of connection to be used when
        connecting to the MySQL server. How the connection is made to the
        MySQL database is determined by the <em><code class="parameter">host</code></em>
        parameter.
       </p>
      </p></blockquote>
     </dd>

    
   </dl>

  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-mysqli.construct-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   Returns an object which represents the connection to a MySQL Server.
  </p>
 </div>


 <div class="refsect1 changelog" id="refsect1-mysqli.construct-changelog">
  <h3 class="title">更新日志</h3>
  <p class="para">
   <table class="doctable informaltable">
    
     <thead>
      <tr>
       <th>版本</th>
       <th>说明</th>
      </tr>

     </thead>

     <tbody class="tbody">
      <tr>
       <td>5.3.0</td>
       <td>
        Added the ability of persistent connections.
       </td>
      </tr>

     </tbody>
    
   </table>

  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-mysqli.construct-examples">
  <h3 class="title">范例</h3>
  <div class="example" id="example-1626">
   <p><strong>Example #1 <span class="methodname"><strong>mysqli::__construct()</strong></span> example</strong></p>
   <div class="example-contents"><p>面向对象风格</p></div>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$mysqli&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">mysqli</span><span style="color: #007700">(</span><span style="color: #DD0000">'localhost'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'my_user'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'my_password'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'my_db'</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">/*<br />&nbsp;*&nbsp;This&nbsp;is&nbsp;the&nbsp;"official"&nbsp;OO&nbsp;way&nbsp;to&nbsp;do&nbsp;it,<br />&nbsp;*&nbsp;BUT&nbsp;$connect_error&nbsp;was&nbsp;broken&nbsp;until&nbsp;PHP&nbsp;5.2.9&nbsp;and&nbsp;5.3.0.<br />&nbsp;*/<br /></span><span style="color: #007700">if&nbsp;(</span><span style="color: #0000BB">$mysqli</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">connect_error</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;die(</span><span style="color: #DD0000">'Connect&nbsp;Error&nbsp;('&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">$mysqli</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">connect_errno&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #DD0000">')&nbsp;'<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">$mysqli</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">connect_error</span><span style="color: #007700">);<br />}<br /><br /></span><span style="color: #FF8000">/*<br />&nbsp;*&nbsp;Use&nbsp;this&nbsp;instead&nbsp;of&nbsp;$connect_error&nbsp;if&nbsp;you&nbsp;need&nbsp;to&nbsp;ensure<br />&nbsp;*&nbsp;compatibility&nbsp;with&nbsp;PHP&nbsp;versions&nbsp;prior&nbsp;to&nbsp;5.2.9&nbsp;and&nbsp;5.3.0.<br />&nbsp;*/<br /></span><span style="color: #007700">if&nbsp;(</span><span style="color: #0000BB">mysqli_connect_error</span><span style="color: #007700">())&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;die(</span><span style="color: #DD0000">'Connect&nbsp;Error&nbsp;('&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">mysqli_connect_errno</span><span style="color: #007700">()&nbsp;.&nbsp;</span><span style="color: #DD0000">')&nbsp;'<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">mysqli_connect_error</span><span style="color: #007700">());<br />}<br /><br />echo&nbsp;</span><span style="color: #DD0000">'Success...&nbsp;'&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">$mysqli</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">host_info&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$mysqli</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">close</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

   <div class="example-contents"><p>面向对象风格 when extending mysqli class</p></div>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br /></span><span style="color: #007700">class&nbsp;</span><span style="color: #0000BB">foo_mysqli&nbsp;</span><span style="color: #007700">extends&nbsp;</span><span style="color: #0000BB">mysqli&nbsp;</span><span style="color: #007700">{<br />&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;function&nbsp;</span><span style="color: #0000BB">__construct</span><span style="color: #007700">(</span><span style="color: #0000BB">$host</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$user</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$pass</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$db</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">parent</span><span style="color: #007700">::</span><span style="color: #0000BB">__construct</span><span style="color: #007700">(</span><span style="color: #0000BB">$host</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$user</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$pass</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$db</span><span style="color: #007700">);<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(</span><span style="color: #0000BB">mysqli_connect_error</span><span style="color: #007700">())&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;die(</span><span style="color: #DD0000">'Connect&nbsp;Error&nbsp;('&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">mysqli_connect_errno</span><span style="color: #007700">()&nbsp;.&nbsp;</span><span style="color: #DD0000">')&nbsp;'<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">mysqli_connect_error</span><span style="color: #007700">());<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}<br /><br /></span><span style="color: #0000BB">$db&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">foo_mysqli</span><span style="color: #007700">(</span><span style="color: #DD0000">'localhost'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'my_user'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'my_password'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'my_db'</span><span style="color: #007700">);<br /><br />echo&nbsp;</span><span style="color: #DD0000">'Success...&nbsp;'&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">$db</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">host_info&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$db</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">close</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

   <div class="example-contents"><p>过程化风格</p></div>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$link&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">mysqli_connect</span><span style="color: #007700">(</span><span style="color: #DD0000">'localhost'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'my_user'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'my_password'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'my_db'</span><span style="color: #007700">);<br /><br />if&nbsp;(!</span><span style="color: #0000BB">$link</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;die(</span><span style="color: #DD0000">'Connect&nbsp;Error&nbsp;('&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">mysqli_connect_errno</span><span style="color: #007700">()&nbsp;.&nbsp;</span><span style="color: #DD0000">')&nbsp;'<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">mysqli_connect_error</span><span style="color: #007700">());<br />}<br /><br />echo&nbsp;</span><span style="color: #DD0000">'Success...&nbsp;'&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">mysqli_get_host_info</span><span style="color: #007700">(</span><span style="color: #0000BB">$link</span><span style="color: #007700">)&nbsp;.&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">mysqli_close</span><span style="color: #007700">(</span><span style="color: #0000BB">$link</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

   <div class="example-contents"><p>以上例程会输出：</p></div>
   <div class="example-contents screen">
<div class="cdata"><pre>
Success... MySQL host info: localhost via TCP/IP
</pre></div>
   </div>
  </div>
 </div>


 <div class="refsect1 notes" id="refsect1-mysqli.construct-notes">
  <h3 class="title">注释</h3>
  <blockquote class="note"><p><strong class="note">Note</strong>: 
<p class="para">MySQLnd 总是使用服务器的默认字符集。此字符集在连接握手／认证时发送，并被 mysqlnd
使用。</p><p class="para">Libmysqlclient 使用 <var class="filename">my.cnf</var> 中的默认字符集或者由在调用
<span class="function"><a href="mysqli.init.html" class="function">mysqli_init()</a></span> 之后，<span class="function"><a href="mysqli.real-connect.html" class="function">mysqli_real_connect()</a></span>
之前先调用 <span class="function"><a href="mysqli.options.html" class="function">mysqli_options()</a></span> 来指定。</p></p></blockquote>
  <blockquote class="note"><p><strong class="note">Note</strong>: 
   <p class="para">
    OO syntax only: If a connection fails an object is still returned. To check 
    if the connection failed then use either the 
    <span class="function"><a href="mysqli.connect-error.html" class="function">mysqli_connect_error()</a></span> function or the <a href="mysqli.connect-error.html" class="link">mysqli-&gt;connect_error</a> property as in 
    the preceding examples.
   </p>
  </p></blockquote>
  <blockquote class="note"><p><strong class="note">Note</strong>: 
   <p class="para">
    If it is necessary to set options, such as the connection timeout,
    <span class="function"><a href="mysqli.real-connect.html" class="function">mysqli_real_connect()</a></span> must be used instead.
   </p>
  </p></blockquote>
  <blockquote class="note"><p><strong class="note">Note</strong>: 
   <p class="para">
    Calling the constructor with no parameters is the same as calling
    <span class="function"><a href="mysqli.init.html" class="function">mysqli_init()</a></span>.
   </p>
  </p></blockquote>
  <blockquote class="note"><p><strong class="note">Note</strong>: 
   <p class="para">
    Error &quot;Can&#039;t create TCP/IP socket (10106)&quot; usually means that the <a href="ini.core.html#ini.variables-order" class="link">variables_order</a> configure directive
    doesn&#039;t contain character <em>E</em>. On Windows, if the
    environment is not copied the <em>SYSTEMROOT</em> environment
    variable won&#039;t be available and PHP will have problems loading Winsock.
   </p>
  </p></blockquote>
 </div>


 <div class="refsect1 seealso" id="refsect1-mysqli.construct-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="mysqli.real-connect.html" class="function" rel="rdfs-seeAlso">mysqli_real_connect()</a> - Opens a connection to a mysql server</span></li>
    <li class="member"><span class="function"><a href="mysqli.options.html" class="function" rel="rdfs-seeAlso">mysqli_options()</a> - Set options</span></li>
    <li class="member"><span class="function"><a href="mysqli.connect-errno.html" class="function" rel="rdfs-seeAlso">mysqli_connect_errno()</a> - Returns the error code from last connect call</span></li>
    <li class="member"><span class="function"><a href="mysqli.connect-error.html" class="function" rel="rdfs-seeAlso">mysqli_connect_error()</a> - Returns a string description of the last connect error</span></li>
    <li class="member"><span class="function"><a href="mysqli.close.html" class="function" rel="rdfs-seeAlso">mysqli_close()</a> - 关闭先前打开的数据库连接</span></li>
   </ul>
  </p>
 </div>


</div><hr /><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="mysqli.connect-error.html">mysqli::$connect_error</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="mysqli.debug.html">mysqli::debug</a></div>
 <div class="up"><a href="class.mysqli.html">MySQLi</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div></body></html>
